Java Object and Class

    Object and Class

    In this tutorial, we will get detailed knowledge about objects, classes, and object-oriented concepts.

    What is an Object in Java?

    An object is an entity that has its state and behavior, which can be logical or physical. For example- car, keyboard, pen, etc.

    An object considered to have below three characteristics-

    • State- specifies the object’s value.
    • Behavior- specifies the object’s functionality.
    • Identity- it can be implemented by the unique ID, which might not be visible to the user. It is mostly used by the JVM to identify the object.

    An object is an instance of the class which holds memory. An object is considered to be a real-world run-time entity.

    What is a Class in Java?

    A class is considered to be a collection of objects that holds the same property. A class is a blueprint that creates an object. Unlike the object, the class is a logical entity but not a physical entity. A class is a collection of-

    • Fields
    • Methods
    • Constructors
    • Blocks
    • Interfaces

    Class Syntax

    class <class_name>{  
    
        Field or variable or constant;  
    
        method;  
    
    }  

    What is an Instance Variable?

    An instance variable is one that is created within the class body but outside the method body. At the compile-time, there is no memory allocated to the instance variable, but at the run-time, when an object is created hence called the instance variable.

    What is a Method in Java?

    A method works like a function that exposes the object’s behavior. It allows you to reuse code and optimize the code.

    What is a New Keyword in Java?

    This keyword allocates the memory at the run-time in the heap memory area.

    Example: Using the main method within the class

    class Student{   
    
     int id;
    
     String stu_name;  
    
     public static void main(String args[]){  
    
      Student s1=new Student();//creating an object 
    
      System.out.println(s1.id);
    
      System.out.println(s1.stu_name);  
    
     }  
    
    }  

    Output

    0
    Null

    As there is no value assigned to the variables, so 0 and null are the output.

    Example: Using the main method outside the class

    class Student{  
     int id;  
     String name;  
    }  
    
    
    class DemoStudent{  
    
     public static void main(String args[]){  
    
      Student s1=new Student();  
    
      System.out.println(s1.id);  
    
      System.out.println(s1.name);  
    
     }  
    
    }  

    Output

    0
    Null

    How can You Initialize the Object?

    You can initialize objects in below three ways-

    • Using the reference variable
    • Using method
    • Using constructor

    Object Initialization Using the Reference Variable

    Example

    class Student{  
      int id;  
      String name;  
    }  
    
    class DemoStudent{  
    
     public static void main(String args[]){  
       Student stu=new Student();  
       stu.id=101;  
       stu.name="Jacob";  
       System.out.println(stu.id+" "+stu.name);
     }  
    }  

    Output

    101 Jacob

    Objects Initialization using a Method

    Example

    class Student{  
    
     int rollno;  
    
     String name;  
    
     void insertDetail(int r, String n){  
    
      rollno=r;  
    
      name=n;  
    
     }  
    
     void displayInformation(){System.out.println(rollno+" "+name);}  
    
    }  
    
    class DemoStudent{  
    
     public static void main(String args[]){  
    
      Student s1=new Student();  
    
      Student s2=new Student();  
    
      s1.insertRecord(1,"Jacob");  
    
      s2.insertRecord(2,"Sam");  
    
      s1.displayInformation();  
    
      s2.displayInformation();  
    
     }  
    
    }  

    Output

    1 Jacob
    2 Sam

    Different ways to create an object in Java

    • Using a new keyword.
    • By newInstance() method
    • By clone() method
    • By deserialization
    • By factory method etc.

    What is an Anonymous Object?

    It is defined as an object with no reference and can be used at the object creation time only. It is a good option to use when you have to use an object only once.

    New DemoStudent();

    You can call a method using the anonymous object:

    New DemoStudent.<method_name>();

    How to create multiple objects of the same type?

    DemoStudent s1= new DemoStudent(), r2=new DemoStudent();